<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ChangeStatus;
use App\Admin\Repositories\Contact;
use App\Models\AdminUser;
use App\Models\Contact as ModelsContact;
use App\Models\Title;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ContactController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Contact(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('phone');
            $grid->column('title')->display(function ($title) {
                return (Title::find($title))['name'];
            })->label(Admin::color()->info());
            $grid->column('note');
            $grid->column('author')->display(function ($author) {
                return (AdminUser::find($author))['name'];
            })->label(Admin::color()->warning());
            $grid->column('status')->using(
                [
                    0 => '无效',
                    1 => '有效'
                ]
            )->badge(
                [
                    0 => 'gray',
                    1 => 'success'
                ]
            );
            $grid->column('updated_at')->sortable();

            $grid->actions( function (Grid\Displayers\Actions $actions) {
                $actions->prepend(new ChangeStatus(ModelsContact::class));
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Contact(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('phone');
            $show->field('title');
            $show->field('note');
            $show->field('author');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Contact(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->mobile('phone')->required()->options(['mask' => '999 9999 9999'])
                ->rules('regex:/^1[3456789]\d{9}$/', [
                    'regex' => '电话号码不正确'
                ]);
            $form->select('title')->options(Title::where(['status' => 1])->pluck('name', 'id'))->required();
            $form->textarea('note');
            // $form->select('author')->options(AdminUser::all()->pluck('name', 'id'))->default(1)->required();
            $form->switch('status')->default(1);

            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function (Form $form) {
                $form->author = Admin::user()->id;
                $form->phone = preg_replace('# #', '', $form->phone);
                return true;
            });
        });
    }
}
